package com.darker.yygh.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import com.darker.yygh.repository.DepartmentRepository;
import com.darker.yygh.service.DepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentRepository departmentRepository;


    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department.getDepname()!=null){
            return department.getDepname();
        }
       return "获取科室名称出错";
    }
    //获得所有科室集合
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> result=new ArrayList<>();
        Department department=new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        //获得所有科室集合
        List<Department> departmentList = departmentRepository.findAll(example);

        //根据大科室编号分组
        Map<String, List<Department>> map = departmentList.stream()
                .collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map
        for(Map.Entry<String, List<Department>> entry:map.entrySet()){
            String bigCode = entry.getKey();
            List<Department> departments = entry.getValue();
            //封装大科室数据
            DepartmentVo departmentVo=new DepartmentVo();
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(departments.get(0).getBigname());
            //封装小科室数据
            List<DepartmentVo> chilren=new ArrayList<>();
            for (Department department1 : departments) {
                    DepartmentVo departmentVo1=new DepartmentVo();
                    departmentVo1.setDepname(department1.getDepname());
                    departmentVo1.setDepcode(department1.getDepcode());
                    chilren.add(departmentVo1);
            }
            departmentVo.setChildren(chilren);
            result.add(departmentVo);
        }

        return result;
    }

    //删除科室
    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.findDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }

    }

    //上传科室接口
    @Override
    public void save(Map<String, Object> map) {
        String s = JSON.toJSONString(map);
        Department department = JSON.parseObject(s, Department.class);
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();
        Department departmentExist = departmentRepository.
                findDepartmentByHoscodeAndDepcode(hoscode, depcode);

        if (departmentExist == null) {
            //数据不存在则添加
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        } else {
            //如果数据库中该数据存在则修改
            departmentExist.setId(departmentExist.getId());
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        }
    }

    @Override
    public List<Department> findByHoscode(String hoscode) {
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> departmentList = departmentRepository.findAll(example);
        return departmentList;
    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        //0为第一页
        Pageable pageable = PageRequest.of(page - 1, limit);
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, exampleMatcher);
        Page<Department> departmentPage = departmentRepository.findAll(example, pageable);
        return departmentPage;
    }


}
